<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\ViewLog;
use App\Models\Article;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;


class CategoryController extends BaseController
{
    //
    public function getList()
    {
        $list = Category::select('id','name','pid','pic','created_at')
            ->where('is_hide', 0)
            ->orderby('sort', 'asc')
            ->get();
        foreach ($list as $k=>$v) {
            $v->pic = $this->http_url.'/uploads/'.$v->pic;
        }


        return $this->success('成功', $list);
    }

    //今日推荐
    public function recommend()
    {
        $title_len = 10;
        $model = ViewLog::selectRaw('article_id, sum(nums) AS nums')
            ->where('date', '<', date('Y-m-d'))
            ->groupBy('article_id', 'date')
            ->orderBy('nums','desc')
            ->take($title_len)
            ->get();
        $arr = [];
        if($model && count($model) == $title_len){
            foreach ($model as $k=>$v){
                array_push($arr,$v->article_id);
            }
            $sql = Article::select('id','title','pic', 'source_link','intro','from_cateid','created_at')
                ->whereIn('id',$arr)
                ->inRandomOrder()
                ->get();
            if(!$sql){
                return $this->error('无效视频id','0');
            }
        }else{
            $sql = Article::select('id', 'pic', 'source_link','title','intro','from_cateid','created_at')
                ->inRandomOrder()
                ->take($title_len)
                ->get();
        }
        foreach ($sql as $k=>$v){
            $v->date = date('m',strtotime($v->created_at)) .'/'. date('d',strtotime($v->created_at));
        }
        return $this->success('成功', $sql);
    }
}
